set more off

insheet using "distance/cntydist.asc", clear

rename v1 BORDINDX // Index of border
rename v2 ST1ST2 // Name of the border
rename v3 ST1 // State 1 of the border pair
rename v4 ST2 // State 2 of the border pair
rename v5 ST
rename v6 COUNTY
rename v7 MINDIST // Minimum distance of county from border.
rename v8 MILEMARK // Point along border where minimum is obtained.
drop v9

tostring COUNTY ST, replace
rename COUNTY fipscounty
rename ST fipsstate
replace fipsstate = "0" + fipsstate if length(fipsstate) == 1
replace fipscounty = "0" + fipscounty if length(fipscounty) == 2
replace fipscounty = "00" + fipscounty if length(fipscounty) == 1
replace fipscounty = fipsstate + fipscounty
destring fipscounty, replace
rename fipscounty fips
rename MINDIST distance
rename ST1ST2 border


#delimit

gen ismainsample =  
	inlist(border, "KY-TN", "VA-WV", "NM-TX", "ND-SD", "ME-NH") |
	inlist(border, "MN-WI", "ID-WA", "ID-OR", "AR-MO", "MT-ND") |
	inlist(border, "AR-OK", "MN-SD", "AR-TN", "AR-MS", "AR-LA") |
	inlist(border, "IL-WI", "IA-MO", "IA-NE", "IL-MO", "CO-KS") |
	inlist(border, "ID-NV", "NV-UT", "AZ-UT", "CO-UT", "CO-WY") |
	inlist(border, "CO-NE", "KY-VA", "KY-MO", "MD-VA", "NM-OK") |
	inlist(border, "CO-OK", "MI-WI", "NM-UT", "AR-TX", "IA-SD", "IA-WI")
	;
	
#delimit cr


destring fipsstate, replace
merge m:1 fipsstate using "expansion/expansion"
keep if _m == 3

preserve
drop _m
rename expansion z
save "dataset_distanceforplacebo", replace
restore

keep if ismainsample == 1
replace distance = -distance if expansion == 0

/*
#delimit;

replace distance = -distance if 
	inlist(fipsstate, "47", "51", "48", "46", "23", "55", "16", "29", "30") |
	inlist(fipsstate, "40", "28", "22", "31", "20", "49", "56")
	;
	
#delimit cr
*/

*** label such that expansion state is second ***

replace border = "TN-KY" if border == "KY-TN"
replace border = "TX-NM" if border == "NM-TX"
replace border = "SD-ND" if border == "ND-SD"
replace border = "NH-ME" if border == "ME-NH"
replace border = "WI-MN" if border == "MN-WI"
replace border = "MO-AR" if border == "AR-MO"
replace border = "OK-AR" if border == "AR-OK"
replace border = "SD-MN" if border == "MN-SD"
replace border = "TN-AR" if border == "AR-TN"
replace border = "MS-AR" if border == "AR-MS"
replace border = "LA-AR" if border == "AR-LA"
replace border = "WI-IL" if border == "IL-WI"
replace border = "MO-IA" if border == "IA-MO"
replace border = "NE-IA" if border == "IA-NE"
replace border = "MO-IL" if border == "IL-MO"
replace border = "KS-CO" if border == "CO-KS"
replace border = "NV-ID" if border == "NV-ID"
replace border = "UT-NV" if border == "NV-UT"
replace border = "UT-AZ" if border == "AZ-UT"
replace border = "UT-CO" if border == "CO-UT"
replace border = "WY-CO" if border == "CO-WY"
replace border = "NE-CO" if border == "CO-NE"
replace border = "VA-KY" if border == "KY-VA"
replace border = "MO-KY" if border == "KY-MO"
replace border = "VA-MD" if border == "MD-VA"
replace border = "OK-NM" if border == "NM-OK"
replace border = "OK-CO" if border == "CO-OK"
replace border = "WI-MI" if border == "MI-WI"
replace border = "UT-NM" if border == "NM-UT"
replace border = "TX-AR" if border == "AR-TX"
replace border = "SD-IA" if border == "IA-SD"
replace border = "WI-IA" if border == "WI-IA"

rename MILEMARK milemarker

local myvars ismainsample fipsstate fips border distance
keep `myvars'
order `myvars'

*** FIX VIRGINIA ***
replace fips = 	51003	if fips == 	51901
replace fips = 	51005	if fips == 	51903
replace fips = 	51019	if fips == 	51909
replace fips = 	51031	if fips == 	51911
replace fips = 	51035	if fips == 	51913
replace fips = 	51053	if fips == 	51918
replace fips = 	51059	if fips == 	51919
replace fips = 	51081	if fips == 	51923
replace fips = 	51089	if fips == 	51929
replace fips = 	51095	if fips == 	51931
replace fips = 	51121	if fips == 	51933
replace fips = 	51143	if fips == 	51939
replace fips = 	51149	if fips == 	51941
replace fips = 	51153	if fips == 	51942
replace fips = 	51161	if fips == 	51944
replace fips = 	51163	if fips == 	51945
replace fips = 	51165	if fips == 	51947
replace fips = 	51175	if fips == 	51949
replace fips = 	51177	if fips == 	51951
replace fips = 	51191	if fips == 	51953
replace fips = 	51015	if fips == 	51907
replace fips = 	51069	if fips == 	51921
replace fips = 	51195	if fips == 	51955
replace fips = 	51199	if fips == 	51958
replace fips = 	51083 	if fips ==	51925

*** reshape ***

gen absdistance = abs(distance)
gsort fips + absdistance
bys fips: gen bordernumber = _n

reshape i fips
reshape j bordernumber
reshape xi fipsstate
reshape xij border distance ismainsample
reshape wide

*** save ***

save "distance/distance", replace
